/*
 * @Description:
 * @Author: wangyan
 * @Date: 2021-03-02 14:33:33
 * @LastEditTime: 2021-03-31 17:55:24
 * @LastEditors: @yzcheng
 */

import {
  pageHistoryList,
  flowTrackByProcessInstanceId,
  getUserAlias,
  getFlowBpmn,
  getTaskCurrent,
  finishTask,
} from '@/api/historyListApi.js'
import { getBpmnById } from '@/api/bpmnApi'
import { listClassify } from '@/api/flowClassifyApi'

export default {
  namespaced: true,
  state: () => ({
    treeData: [],
  }),
  getter: {},
  actions: {
    async pageHistoryList(context, payload) {
      console.log(payload)
      const rst = await pageHistoryList(payload.pageOption, payload.data)
      if (rst.data.code == 200) {
        return rst.data.data
      } else {
        return false
      }
    },
    async flowTrackByProcessInstanceId(context, payload) {
      let rst = await flowTrackByProcessInstanceId(payload)
      rst = rst.data
      if (rst.success) {
        let res = await getUserAlias(
          rst?.data?.map((item) => 'usernameList=' + item.assignee)
        )
        res = res.data
        return rst?.data?.map((val) => {
          return Object.assign(val, {
            userName: res?.data?.filter((v) => v.username === val.assignee)?.[0]?.cname,
          })
        })
      } else {
        return false
      }
    },
    async getFlowBpmn(context, payload) {
      let rst = await getFlowBpmn(payload)
      rst = rst.data
      if (rst.success) {
        return rst.data
      } else {
        return false
      }
    },
    async getBpmnXML(context, payload) {
      let rst = await getBpmnById(payload)
      rst = rst.data
      if (rst.success) {
        return rst.data
      } else {
        return false
      }
    },
    async getTaskCurrent(context, payload) {
      let rst = await getTaskCurrent(payload)
      rst = rst?.data
      if (rst?.success) {
        return rst?.data
      } else {
        return false
      }
    },
    async getFlowList(payload, { state }) {
      let rst = await listClassify(payload)
      rst = rst.data
      if (rst.success) {
        state.treeData = rst.data
        return rst.data
      } else {
        return false
      }
    },
    async finishTask(context, payload) {
      let rst = await finishTask(payload)
      rst = rst.data
      if (rst.success) {
        console.log(rst)
        return rst
      } else {
        return false
      }
    },
  },
  mutations: {},
}
